package cn.com.xjnt.vas.ordering.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.frameworks.core.repository.BaseRepository;
import com.frameworks.core.service.AbstractBaseService;

import cn.com.xjnt.vas.ordering.domain.entity.FoodOrder;
import cn.com.xjnt.vas.ordering.domain.entity.User;
import cn.com.xjnt.vas.ordering.repository.FoodOrderRepository;
import cn.com.xjnt.vas.ordering.service.FoodOrderService;

/** 
* 类说明：
* @author 作者 LUXIN
* @version 创建时间：2016年5月17日17:19:21
*/
@Service
@Transactional
public class FoodOrderServiceImpl extends AbstractBaseService<FoodOrder, Long> implements FoodOrderService {
    @Autowired
    private FoodOrderRepository foodOrderRepository;
    
    @Override
    protected BaseRepository<FoodOrder, Long> getBaseRepository() {
        return foodOrderRepository;
    }

    @Override
    public void save(FoodOrder entity) {
        super.saveOrUpdate(entity);
    }

    @Override
    public void update(FoodOrder entity) {
        super.saveOrUpdate(entity);
    }

    @Override
    public void saveEntity(FoodOrder entity) {
        String orderNo =  new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()) + new Random().nextInt(5);
        entity.setOrderno(orderNo);
        entity.setType(0);
        save(entity);
    }

    @Override
    public void updateOrderType(FoodOrder entity) {
        FoodOrder model = getBaseRepository().findOne(entity.getId());
        switch (entity.getType()) {
        case 3://配送中
            //配送时间
            model.setDispatchTime(entity.getDispatchTime());
            //预计到达时间
            model.setPreFinishTime(entity.getPreFinishTime());
            break;
        case 4://订单已完成
            //实际到达时间
            model.setRealFinishTime(entity.getRealFinishTime());
            break;
        case 5://取消订单
            //取消原因
            model.setReason(entity.getReason());
            break;
        default://1：等待商家确认；2：商家已确认
            break;
        }
        model.setType(entity.getType());
        update(model);
    }

    @Override
    public List<FoodOrder> myCart(User user,int type) {
        return foodOrderRepository.findByUserAndType(user, type);
    }

    @Override
    public List<FoodOrder> myOrder(User user) {
        return foodOrderRepository.findMyOrder(user, 0);
    }
}
 